﻿Public Class CallHistory
    Public Function GetCallHistory(ByVal Start As Integer, ByVal Limit As Integer) As String
        Dim objCallHistory As New OpenNETCF.Phone.CallLog

        Dim ListCallHistory As New CallEntries

        Dim AllList = (From u As OpenNETCF.Phone.CallLogEntry In objCallHistory _
                       Order By u.StartTime Descending _
                       Select New CallEntry With {.Name = u.Name, .CallType = u.CallType.ToString, .EndTime = u.EndTime.ToString, .Number = u.Number, .StartTime = u.StartTime.ToString, .NameType = u.NameType}).ToList

        ListCallHistory.Success = True
        ListCallHistory.Rows = (From u In AllList _
                               Select u _
                               Skip Start _
                               Take Limit).ToList

        ListCallHistory.Results = AllList.Count
        'Dim I As String = ToJson(ListCallHistory)
        Return ToJson(ListCallHistory)
    End Function

    Public Function GetMissedCalls(ByVal Start As Integer, ByVal Limit As Integer) As String
        Dim objCallHistory As New OpenNETCF.Phone.CallLog

        Dim ListMissedCalls As New CallEntries

        Dim AllList = (From u As OpenNETCF.Phone.CallLogEntry In objCallHistory _
                       Where u.CallType = OpenNETCF.Phone.CallType.Missed _
                       Order By u.StartTime Descending _
                       Select New CallEntry With {.NameType = u.NameType, .CallType = u.CallType.ToString, .EndTime = u.EndTime.ToString, .Name = u.Name, .Number = u.Number, .StartTime = u.StartTime.ToString}).ToList

        ListMissedCalls.Rows = (From u In AllList _
                               Select u _
                               Skip Start _
                               Take Limit).ToList

        ListMissedCalls.Results = AllList.Count

        Return ToJson(ListMissedCalls)
    End Function

    Public Function GetOutgoingCalls(ByVal Start As Integer, ByVal Limit As Integer) As String
        Dim objCallHistory As New OpenNETCF.Phone.CallLog
        Dim ListOutgoingCalls As New CallEntries

        Dim AllList = (From u As OpenNETCF.Phone.CallLogEntry In objCallHistory _
                       Where u.CallType = OpenNETCF.Phone.CallType.Outgoing _
                       Order By u.StartTime Descending _
                       Select New CallEntry With {.NameType = u.NameType, .CallType = u.CallType.ToString, .EndTime = u.EndTime.ToString, .Name = u.Name, .Number = u.Number, .StartTime = u.StartTime.ToString}).ToList

        ListOutgoingCalls.Rows = (From u In AllList _
                               Select u _
                               Skip Start _
                               Take Limit).ToList

        ListOutgoingCalls.Results = AllList.Count

        Return ToJson(ListOutgoingCalls)
    End Function

    Public Function GetIncomingCalls(ByVal Start As Integer, ByVal Limit As Integer) As String
        Dim objIncomingCalls As New OpenNETCF.Phone.CallLog
        
        Dim ListIncomingCalls As New CallEntries

        Dim AllList = (From u As OpenNETCF.Phone.CallLogEntry In objIncomingCalls _
                       Where u.CallType = OpenNETCF.Phone.CallType.Incoming _
                       Order By u.StartTime Descending _
                       Select New CallEntry With {.NameType = u.NameType, .CallType = u.CallType.ToString, .EndTime = u.EndTime.ToString, .Name = u.Name, .Number = u.Number, .StartTime = u.StartTime.ToString}).ToList

        ListIncomingCalls.Rows = (From u In AllList _
                               Select u _
                               Skip Start _
                               Take Limit).ToList

        ListIncomingCalls.Results = AllList.Count
        Return ToJson(ListIncomingCalls)
    End Function

End Class
